import 'package:common/common.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:miaowu/app/global_web/global_web_page.dart';
import 'package:miaowu/app/router/ext_get_navigation.dart';

import 'public_alert_widget.dart';
import 'public_btn_tem.dart';

/// description
typedef PublicButtonWidgetCallBack = void Function(PublicBtnItem btnItem);

class PublicButtonWidget extends StatelessWidget {
  const PublicButtonWidget(
      {super.key,
      required this.btnItem,
      required this.child,
      this.alertCallBack,
      this.clickAction,
      this.ignoreKey,
      this.color = Colors.transparent});
  final PublicBtnItem btnItem;
  final Widget child;
  final PublicAlertWidgetClickCakkBack? alertCallBack; // 弹框回调
  final PublicButtonWidgetCallBack? clickAction; // 点击直接执行
  final Color color;
  final String? ignoreKey; // 设置该key忽略内部处理方法，回调外部处理
  @override
  Widget build(BuildContext context) {
    return btnItem.view == 1
        ? GestureDetector(
            onTap: () {
              logs('我惦记了按钮');

              publicCheckClickAction(
                btnItem: btnItem,
                ignoreKey: ignoreKey,
                alertCallBack: alertCallBack,
                clickAction: clickAction,
              );

              // BtnClick? click = btnItem.click;
              // if (click == null) {
              //   return;
              // }
              // if (click.react == 'window') {
              //   SmartDialog.show(
              //     usePenetrate: false,
              //     clickMaskDismiss: true,
              //     backType: SmartBackType.block,
              //     keepSingle: true,
              //     useAnimation: false,
              //     builder: (context) => Center(
              //       child: PublicAlertWidget(
              //         windowItem: click.window!,
              //         callBack: (WindowBtn item) {
              //           alertCallBack?.call(item);
              //         },
              //       ),
              //     ),
              //   );
              // } else {
              //   clickAction.call(btnItem);
              // }
            },
            behavior: HitTestBehavior.opaque,
            child: Container(color: color, child: child))
        : const SizedBox();
  }
}

void publicCheckClickAction(
    {required PublicBtnItem btnItem,
    String? ignoreKey,
    PublicAlertWidgetClickCakkBack? alertCallBack,
    PublicButtonWidgetCallBack? clickAction}) {
  BtnClick? click = btnItem.click;

  if (ignoreKey != null && ignoreKey == btnItem.key) {
    clickAction?.call(btnItem);
    return;
  }
  if (click == null) {
    return;
  }
  if (click.react == 'window') {
    SmartDialog.show(
      usePenetrate: false,
      clickMaskDismiss: true,
      backType: SmartBackType.block,
      keepSingle: true,
      useAnimation: false,
      builder: (context) => Center(
        child: PublicAlertWidget(
          windowItem: click.window!,
          callBack: (WindowBtn item) {
            alertCallBack?.call(item);
          },
        ),
      ),
    );
  }
  if (click.react == 'jump') {
    if (btnItem.key == 'jumpH5') {
      GlobalWebPage.startInstance(btnItem.title ?? '', click.jump ?? '');
      return;
    }
    String router = '/${click.option!}';
    Get.pushPage(router);
  } else {
    clickAction?.call(btnItem);
  }
}
